package com.yd.app.mapper.provider;

import com.yd.domain.searchParam.OrderParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 订单
 * Created by sunxianhui on 2018/3/7.
 */
public class OrderProvider extends BaseProvider {
    /**
     * 获取每一页列表
     * @param map
     * @return
     */
    public String searchOrderList(Map map) {
        OrderParam orderParam = (OrderParam) map.get("orderParam");
        StringBuilder sb = new StringBuilder("SELECT o.*,u.realName as userName,u.companyName  FROM business_order o left join sys_user u on o.userId=u.id where 1=1   ");
        sb.append(searchOtherSql(orderParam));
        sb.append(" order by createTime desc ");
        sb.append(" limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 获取查询总数量
     * @param map
     * @return
     */
    public String searchOrderCount(Map map) {
        OrderParam orderParam = (OrderParam) map.get("orderParam");
        StringBuilder sb = new StringBuilder(" select count(1) from business_order o left join sys_user u on o.userId=u.id where 1=1");
        sb.append(searchOtherSql(orderParam));
        return sb.toString();
    }

    /**
     * 拼装查询条件
     * @param orderParam
     * @return
     */
    public String searchOtherSql(OrderParam orderParam) {
        StringBuilder sb = new StringBuilder();
        if(!StringUtils.isEmpty(orderParam.getRealName())&&!"".equals(orderParam.getRealName())){
            sb.append(" and u.realName like" +" '%"+orderParam.getRealName().trim()+"%'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getUserId())&&!"".equals(orderParam.getUserId())){
            sb.append(" and o.userId ='"+orderParam.getUserId()+"'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getCompanyName())&&!"".equals(orderParam.getCompanyName())){
            sb.append(" and u.companyName like" +" '%"+orderParam.getCompanyName().trim()+"%'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getExpressCode())&&!"".equals(orderParam.getExpressCode())){
            sb.append(" and o.expressCode ='"+orderParam.getExpressCode().trim()+"'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getOrderId())&&!"".equals(orderParam.getOrderId())){
            sb.append(" and o.orderId ='"+orderParam.getOrderId()+"'  ");
        }
        if(!orderParam.getIfPC().equals("PC")){
            if(!StringUtils.isEmpty(orderParam.getUserId())&&!"".equals(orderParam.getUserId())){
                sb.append(" and o.userShow =true ");
            }else{
                sb.append(" and o.managerShow =true ");
            }
        }
        if (!StringUtils.isEmpty(orderParam.getStartTime())) {
            sb.append(" AND o.createTime >= #{orderParam.startTime}  ");
        }
        if (!StringUtils.isEmpty(orderParam.getEndTime())) {
            sb.append(" AND o.createTime <= #{orderParam.endTime}  ");
        }
        if(!StringUtils.isEmpty(orderParam.getPayStatus())){
            sb.append(" and o.payStatus ='"+orderParam.getPayStatus()+"'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getStatus())){
            sb.append(" and o.status ='"+orderParam.getStatus()+"'  ");
        }
        if(!StringUtils.isEmpty(orderParam.getQuarter())&&orderParam.getQuarter()>0){
            sb.append(" and quarter(from_unixtime(o.createTime/1000)) ='"+orderParam.getQuarter()+"'  ");
        }
        return sb.toString();
    }
}
